// pages/index/shopInfo.js
var app = getApp();
var regionData = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    loading: 0,
    disabled: 0,
    type:2,
    order_sn: "",
    userInfo: {},
    bankRegion: [],
    bankRegionCode:[],
    regionIndex: [0, 0],
    user_tag: 0,
    bankList:[],
    uploadlist:[],
    provinceArray:[],
    regionArray:[[],[]],
    regionArray2: [[], [], []],
    regionIndex2: [0, 0,0],
    industryList: [["实体","虚拟"],[]],
    industryIndex:[0,0],
    feeValueList: [],
    feeValueIndex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type){
      this.data.type = options.type;
    }
   
    app.getUserInfo((info) => {
      this.data.userInfo = info
      this.getFeeValueList();
    })
    this.getBank();
    this.getBankArea();
    this.getBankSubbranch();
    this.getIndustryList("实体");
    this.getIndustryList("虚拟");
    
  },
  getFeeValueList: function () {
    var that = this;
    app.api("apiv2/mch_my_bank/getFeeList", {
      pid: this.data.userInfo.user_id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.data.rawFeeValueList = res.data;
      var list =[];
      for (var i in res.data){
        list.push(res.data[i].text);
      }
      // console.log(list);
      that.setData({ feeValueList: list});
    });
  },
  bindfeeValueChange:function(e){
    console.log(e);
    var index = e.detail.value;
    var data = this.data.rawFeeValueList[index];
    this.setData({ feeValueText: data.text, feeValue:data.value});
  },
  getIndustryList: function (shopType="") {
    var that = this;
    var data = {};

    data.accountType = this.data.type== 3 ? '企业' : '个体工商户';
    data.shopType = shopType;
    app.api("apiv2/industry/get", data, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      var list = [];
      for (var i in res.data) {
        list.push(res.data[i].industryName);
      }
      if (shopType=="实体"){
        that.data.industryList[1] = list;
        that.setData({ industryList: that.data.industryList});
        that.data.rawIndustryList1 = res.data;
        that.data.industryList1 = list;
      }else{
        that.data.rawIndustryList2 = res.data;
        that.data.industryList2 = list;
      }
    });
  },
  bindIndustryPickerColumnChange:function(e){
    if (e.detail.column != 0) {
      return;
    }
    var that = this;
    console.log(e.detail.value);
   if(e.detail.value==0){
     that.data.industryList[1] = that.data.industryList1 ;
     that.setData({ industryList: that.data.industryList, industryIndex: [e.detail.value,0]});
   }else{
     that.data.industryList[1] = that.data.industryList2;
    //  console.log(that.data.industryList2);
     that.setData({ industryList: that.data.industryList, industryIndex: [e.detail.value, 0] });
   }
  },
  bindIndustryChange:function(e){
    console.log(e);
    var value = e.detail.value;
    if (value[0]==0){
      this.data.industryId = this.data.rawIndustryList1[value[1]].industryId;
      this.data.industryName = this.data.rawIndustryList1[value[1]].industryName;
    }else{
      this.data.industryId = this.data.rawIndustryList2[value[1]].industryId;
      this.data.industryName = this.data.rawIndustryList2[value[1]].industryName;
    }
    this.setData({ industryName: this.data.industryName});
  },

  // 选择银行
  getBank: function () {
    var that = this;
    app.api("apiv2/bank/getBank", {

    }, function (res) {

      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.data.rawBankList = res.data;
      var bankList =[];
      for (var i in res.data){
        bankList.push(res.data[i].bankName);
      }
      that.setData({ bankList: bankList});
    });
  },
  bindBankIdChange:function(e){
    var index =  e.detail.value;
    var bankId = this.data.rawBankList[index].bankId;
    var bankName = this.data.rawBankList[index].bankName;
    this.setData({ bankId: bankId, bankName: bankName });
    this.getBankSubbranch();

  },
  getBankSubbranch: function () {
    var that = this;
    var data = {};
    if (!(that.data.bankId && that.data.bankRegionCode.length==2)){
      return ;
    }
    data.bankId = that.data.bankId;
    data.city_code = that.data.bankRegionCode[1];
    app.api("apiv2/bank/getBankSubbranch", data, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.data.rawBankSubbranchList = res.data;
      var bankSubbranchList =[];
      for (var i in res.data){
        bankSubbranchList.push(res.data[i].sub_bankName);
      }
      that.setData({ bankSubbranchList: bankSubbranchList});
    });
  },
  bindbankSubbranchChange:function(e){
   var index = e.detail.value;
    var data = this.data.rawBankSubbranchList[index];
  //  var contactLineName = data.sub_bankName;
   this.setData({
     contactLineName: data.sub_bankName,
     contactLine: data.contactLine,
     contactLineIndex:index
   });


  },
  // 威付通省市区开户地联动
  getBankArea: function (pid=1,type=1) {
    var that = this;
    app.api("apiv2/bank/render", {
      action: "getChilds",
      pid: pid,
      method:"post"
    }, function (res) {
      if (!res.success) {
        app.alert(res.error);
        return;
      }
     
      // var regionArray = [[],[]];
      regionData[pid] = res.data;
        if (pid==1){
          that.data.provinceArray =res.data;
          for (var i in res.data) {
            that.data.regionArray[0].push(res.data[i].areaName);
            that.data.regionArray2[0].push(res.data[i].areaName);
            that.getBankArea(res.data[i].areaCode);
          }
          that.setData({ regionArray: that.data.regionArray });
        } else if (pid =='010000'){
          if (that.data.regionArray[1].length==0){
            for (var i in res.data) {
              that.data.regionArray[1].push(res.data[i].areaName);
              that.data.regionArray2[1].push(res.data[i].areaName);
              if (res.data[i].areaCode =="010100"){
                 that.getBankArea(res.data[i].areaCode,3);
              }
            }
          }
          that.setData({ regionArray: that.data.regionArray, regionArray2: that.data.regionArray2 });
        } else if (type == 3) {
          that.data.regionArray2[2]=[];
          for (var i in res.data) {
            that.data.regionArray2[2].push(res.data[i].areaName);
            that.setData({  regionArray2: that.data.regionArray2 });
          }
        }
    });
  },
  // 威付通省市区开户地联动
  bindRegionPickerColumnChange:function(e){
    // console.log(e);
    if (e.detail.column!=0){
      return ;
    }
    var index = e.detail.value;
    var pid = this.data.provinceArray[index].areaCode;
    var area = regionData[pid];
    // console.log(pid, index, regionData);
    this.data.regionArray[1]=[];
    for (var i in area) {
      this.data.regionArray[1].push(area[i].areaName);
    }
    this.setData({ regionArray: this.data.regionArray, regionIndex: [index, 0]});
  },
  // 选择银行地址
  bindRegionChange: function (e) {
    var index = e.detail.value;
    var pid = this.data.provinceArray[index[0]].areaCode;
    var provinceName = this.data.provinceArray[index[0]].areaName;
    var cityName = regionData[pid][index[1]].areaName;
    var cityCode = regionData[pid][index[1]].areaCode;
    var bankRegionCode = [pid, cityCode];
    var region = [provinceName, cityName];
    // console.log(region, bankRegionCode);
    this.setData({ bankRegion: region, bankRegionCode: bankRegionCode});
    this.getBankSubbranch();
  },

  bindRegionPickerColumnChange2: function (e) {
    // console.log(e);
    var column = e.detail.column;
    var index = e.detail.value;
    if (column==2){
      return;
    }
     

    if (column == 1) {
      var pid = this.data.province_code;
      var area = regionData[pid];
      this.data.city_code = area[index];
      this.getBankArea(area[index].areaCode,3);
      return;
    }
    
    var pid = this.data.provinceArray[index].areaCode;
    this.data.province_code = pid;
    var area = regionData[pid];
    this.data.regionArray2[1] = [];
    for (var i in area) {
      this.data.regionArray2[1].push(area[i].areaName);
    }
    this.setData({ regionArray2: this.data.regionArray2, regionIndex2: [index, 0,0] });
    this.getBankArea(area[0].areaCode, 3);
  },
  // 选择营业地址
  bindRegionChange2: function (e) {
    var index = e.detail.value;
    var pid = this.data.provinceArray[index[0]].areaCode;
    var provinceName = this.data.provinceArray[index[0]].areaName;
    var cityName = regionData[pid][index[1]].areaName;
    var cityCode = regionData[pid][index[1]].areaCode;
    var countyName = regionData[cityCode][index[2]].areaName;
    var countyCode = regionData[cityCode][index[2]].areaCode;
    var areaRegionCode = [pid, cityCode, countyCode];
    var region = [provinceName, cityName, countyName];
    // console.log(region, bankRegionCode);
    this.setData({ areaRegion: region, areaRegionCode: areaRegionCode });
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  uploadimg:function(e){
     
    this.data.uploadName = e.currentTarget.dataset.name;
    this.data.detectType = e.currentTarget.dataset.type;
    var that = this;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        if (that.data.detectType ){
          that.detectFile(tempFilePaths);
        }
        that.uploadFile(tempFilePaths)
      }
    })
  },
// 网商银行
  uploadFile: function (tempFilePaths){
    var that = this;
    wx.uploadFile({
      url: app.data.apiHost + 'apiv2/mch_my_bank/upload', //威付通接口地址
      filePath: tempFilePaths[0],
      name: 'filedata',
       formData: {
        'type': this.data.detectType
      },
      success(res1) {
        const data = res1.data
        var res = JSON.parse(data);
        if (res.error==0) {
          // that.setDetectData(res.data);
          var thisData = that.data;
          thisData[that.data.uploadName] = tempFilePaths[0]
          thisData.uploadlist[that.data.uploadName] = res['name'];
          that.setData({
            data: thisData 
          })
        }else{
           app.alert(res.msg);
        }
      }
    })
  },
  detectFile: function (tempFilePaths) {
    console.log(tempFilePaths);
    var that = this;
    wx.uploadFile({
      url: app.data.apiHost + 'apiv2/qcloud_image/detect', //
      // url: 'http://tenement.com/apiv2/qcloud_image/detect', //仅为示例，非真实的接口地址
      filePath: tempFilePaths[0],
      name: 'file',
      formData: {
        'type': this.data.detectType
      },
      success(res1) {
        const data = res1.data
        var res = JSON.parse(data);
        if (res.code==200){
          that.setDetectData(res.data);
        }
        //do something
      }
    })
  },

  setDetectData: function (detectData){
    console.log(detectData, this.data.detectType);
    var data = this.data.data;
    if (this.data.detectType =="idcardDetectA"){
      data.principal = detectData.name;
      data.idCode = detectData.id;
    } else if (this.data.detectType == "bizlicense") {
      data.merchantName = detectData['公司名称'];
      data.merchantShortName = detectData['公司名称'];
      data.businessLicense = detectData['注册号'];
      data.address = detectData['地址'];
    } else if (this.data.detectType == "bankcard"){
      data.accountCode = detectData['卡号'];
    }
    this.setData({"data":data});
    console.log(this.data.data);
  },
  setVal: function (e) {
    var name = e.currentTarget.dataset.name;
    var v = e.detail.value;
    var data = this.data.data;
    data[name] = v;
    this.setData({
      data: data
    });
  },

  formSubmit: function (e) {
    
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getShopInfo();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})